package com.yoomiito.app.ui;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import android.widget.FrameLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.R;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.widget.SlideRecyclerView;
import com.yoomiito.app.widget.SpacesItemDecoration;
import com.yoomiito.app.widget.TitleView;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.StatusManager;

import butterknife.BindView;

/**
 * 所有只有列表的界面的base
 */
public abstract class RecyclerViewBaseActivity<P extends BaseActPresenter> extends BaseActivity<P> {
    @BindView(R.id.tv_title)
    protected TitleView mTitleView;
    @BindView(R.id.act_my_list_fmLayout)
    FrameLayout mFrameLayout;

    //    @BindView(R.id.rv)
    protected SlideRecyclerView mRecyclerView;
    //    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout mSmartRefreshLayout;

    protected int currentPage = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_list;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mStatusManager = new StatusManager.Builder(this)
                .addType(new ContentType(R.layout.item_refresh_slide_rcy))
                .addType(new EmptyType().setEmptyContent(getEmptyContent()))
                .build();
        mRecyclerView = mStatusManager.view().findViewById(R.id.rv);
        mSmartRefreshLayout = mStatusManager.view().findViewById(R.id.refreshLayout);
        mFrameLayout.addView(mStatusManager.view());
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
//        mRecyclerView.addItemDecoration(new MyItemDecoration(context, RecyclerView.VERTICAL));
//        mRecyclerView.setLayoutManager(linearLayoutManager);

        if (!useMyItemDecoration()){
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            mRecyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dp_10)));
            mRecyclerView.setLayoutManager(linearLayoutManager);
        }

        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 1;
            getNewData();
        });

        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage++;
            getNewData();
        });
        mSmartRefreshLayout.autoRefresh();
    }

    protected boolean useMyItemDecoration(){
        return false;
    }

    protected abstract void getNewData();

    //空界面 文案
    public abstract String getEmptyContent();
}
